local _G = GLOBAL
local TheSim = _G.TheSim
local TheNet = _G.TheNet
local net_byte = GLOBAL.net_byte
local IsServer = TheNet:GetIsServer() or TheNet:IsDedicated()
local TUNING = _G.TUNING
local require = _G.require
local stackable_replica = require "components/stackable_replica"
local Vector3 = _G.Vector3
local world = _G.TheShard:GetShardId() == "4" and "资源世界" or "主世界"


if GetModConfigData("Created_Krampus") then modimport("scripts/Krampus_create.lua") end--小偷包制作
if GetModConfigData("Cool_Krampus") then modimport("scripts/Krampus_refresh.lua") end--小偷包反鲜变量
if GetModConfigData("Unlimitedsalt") then modimport("scripts/Unlimitedsalt.lua") end--舔盐无耐久
if GetModConfigData("NO_GRASSGEKKO") then modimport("scripts/NO_GRASSGEKKO.lua") end--无草蜥蜴
if GetModConfigData("NoAntlionAttack") then modimport("scripts/NoAntlionAttack.lua") end--无蚁狮攻击
if GetModConfigData("Unlimitedheatrock") then modimport("scripts/Unlimitedheatrock.lua") end--暖石无耐久
local size = GetModConfigData("Maxstacksize")--物品叠加数量
if GetModConfigData("Auto_Stack") then modimport("scripts/Auto_Stack.lua") end--自动堆叠
if GetModConfigData("klausset") then modimport("scripts/klausset.lua") end--赃物袋制作
if GetModConfigData("Foreverbeefalo") then modimport("scripts/Foreverbeefalo.lua") end--驯化的牛不退化
if GetModConfigData("NoGreeting") then modimport("scripts/NoGreeting.lua") end--驯化的牛不打招呼
if GetModConfigData("Fasthand") then modimport("scripts/Fasthand.lua") end--快速工作
if GetModConfigData("MarkedWormhole") then modimport("scripts/MarkedWormhole.lua") end--虫洞标记
TUNING.TENT_USES = GetModConfigData("Tent_uses")--夜间帐篷可使用次数
TUNING.SIESTA_CANOPY_USES = GetModConfigData("Siesta_uses")--午睡帐篷可使用次数
if GetModConfigData("Fast_Travel") then modimport("scripts/Fast_Travel.lua") end--快速传送


AddPrefabPostInit("dug_rock_avocado_bush", function(inst)    
    if GLOBAL.TheWorld.ismastersim then
        inst:AddTag("irreplaceable")    --这个标签会防止被猴子、食人花、坎普斯等拿走
		inst:AddTag("nonpotatable") --这个貌似是使其下线时会自动掉落
    end
end)

--物品叠加数量
TUNING.STACK_SIZE_LARGEITEM = size
TUNING.STACK_SIZE_MEDITEM = size
TUNING.STACK_SIZE_SMALLITEM = size 

-- 定义函数
local stackable_replica_ctorBase = stackable_replica._ctor or function() return true end    
function stackable_replica._ctor(self, inst)
    self.inst = inst

    self._stacksize = net_byte(inst.GUID, "stackable._stacksize", "stacksizedirty")
    self._maxsize = size
end

local stackable_replicaSetMaxSize_Base = stackable_replica.SetMaxSize or function() return true end
function stackable_replica:SetMaxSize(maxsize)
    self._maxsize = size
end

local stackable_replicaMaxSize_Base = stackable_replica.MaxSize or function() return true end
function stackable_replica:MaxSize()
    return self._maxsize
end

local function addStackable(inst)
    if(inst.components.stackable == nil) then
        inst:AddComponent("stackable")
    end
end

if IsServer then

    AddPrefabPostInit("pondeel", addStackable)
    AddPrefabPostInit("pondfish", addStackable)

    AddPrefabPostInit("rabbit",function(inst)
        if(inst.components.stackable == nil) then
            inst:AddComponent("stackable")
        end
        inst.components.inventoryitem:SetOnDroppedFn(function(inst)
            inst.components.perishable:StopPerishing()
            inst.sg:GoToState("stunned")
            if inst.components.stackable then
                while inst.components.stackable:StackSize() > 1 do
                    local item = inst.components.stackable:Get()
                    if item then
                        if item.components.inventoryitem then
                            item.components.inventoryitem:OnDropped()
                        end
                        item.Physics:Teleport(inst.Transform:GetWorldPosition() )
                    end
                end
            end
        end)
    end)
end

--击杀和出现公告
local GongGao_Shang = { 
    "deerclops",        --雪巨鹿
    "bearger",          --比尔熊
    "dragonfly",        --龙蜻蜓
    "moose",            --鹿角鹅
    "klaus",            --克劳斯
    "toadstool",        --毒蟾蜍
	"toadstool_dark",	--悲苦蟾蜍
    "beequeen",         --蜂王后
    "glommer",          --格罗姆
    "koalefant_summer", --考拉夏
    "koalefant_winter", --考拉冬
    "spiderqueen",      --蜘蛛王
    "warg",             --座狼王
    "spat",             --口水羊
	"shadow_rook",      --暗影战车
	"elecarmet",		--电气BOSS
	"antlion",			--蚁狮
	"stalker_atrium",	--远古织影者
	"krampus",			--监控小偷掉落
	"malbatross",       --邪天翁
	"twister",          --豹卷风
	"kraken",			--乌贼王
	"tigershark",		--虎鲨
}

local function GetInstName(inst)
    return inst and inst:GetDisplayName() or "*无名*"
end

local function GetAttacker(data)
    return data and data.attacker and data.attacker:GetDisplayName() or "*无名*"
end

local function diaoluo(inst)
	if inst.components.lootdropper then
		local old = inst.components.lootdropper.GenerateLoot
		inst.components.lootdropper.GenerateLoot = function(self)
			local loots = old(self) or {}
				for i, v in ipairs(loots) do  
					if v and v == "krampus_sack" then
						local name = inst.killedname ~= nil and inst.killedname or "没有击杀者"
						TheNet:Announce("【 "..name.." 】喜提新款克劳斯大包一份")
					end
				end
			return loots
		end
	end
end

AddPrefabPostInit("krampus", diaoluo)

for k,v in pairs(GongGao_Shang) do
    AddPrefabPostInit(v, function (inst)
        if not IsServer then
            return inst
        end
        local function siwangongao(inst, data)
            local AnnNR = v == "glommer" and ("可怜的〖 "..GetInstName(inst).." 〗被可恶的【 "..GetAttacker(data).." 】杀死了") or
                          v == "klaus" and (inst.kaimiao or 0)==1 and ("【 "..GetAttacker(data).." 】解开了〖 "..GetInstName(inst).." 〗锁链") or
						  ("【 "..GetAttacker(data).." 】给了〖 "..GetInstName(inst).." 〗最后一击")
            if (v =="klaus" and (inst.kaimiao or 0) ~= 2) or not inst.kaimiao then if v ~= "krampus"  then TheNet:Announce(AnnNR) else inst.killedname = GetAttacker(data) end end
            if v =="klaus" and inst.kaimiao and inst.kaimiao == 1 then inst.kaimiao = inst.kaimiao + 1 end
        end

        local function siwang(inst)
            if v == "klaus" then if not inst.kaimiao then inst.kaimiao = 0 end inst.kaimiao = inst.kaimiao + 1 end
            if (v=="klaus" and (inst.kaimiao or 0) ~= 3) or not inst.kaimiao then inst:ListenForEvent("attacked", siwangongao) end
        end

        inst:ListenForEvent("death", siwang)

        if v == "glommer" then
            local function beigongji(inst, data)
                TheNet:Announce("〖 "..GetInstName(inst).." 〗正在惨遭【 "..GetAttacker(data).." 】的攻击")
            end
            --local OldOnSpawnFuel = OnSpawnFuel
            inst:ListenForEvent("attacked", beigongji)
			inst:DoTaskInTime(.5, function(inst)
                local x,y,z=inst.Transform:GetWorldPosition()
                local XianZuoBiao = ("，坐标："..(math.floor(x))..", "..(math.floor(z))) or ""
                TheNet:Announce(world.."【 "..GetInstName(inst).." 】出现了，快去领回家！"..XianZuoBiao)
            end)
        end
		
		
		if v == "malbatross" then
			inst:DoTaskInTime(.5, function(inst)
            local x,y,z=inst.Transform:GetWorldPosition()
            local XianZuoBiao = ("，坐标："..(math.floor(x))..", "..(math.floor(z))) or ""
            TheNet:Announce(world.."的【邪天翁】出现了！"..XianZuoBiao)
			end)
		end
    end)
end


----格罗姆花采集
AddPrefabPostInit("statueglommer", function (inst)
    if not _G.TheWorld.ismastersim then
        return inst
    end

    local function OnPicked(inst, picker, loot)
        local glommer = TheSim:FindFirstEntityWithTag("glommer")
        if glommer ~= nil and glommer.components.follower.leader ~= loot then
            glommer.components.follower:StopFollowing()
            glommer.components.follower:SetLeader(loot)
        end
        TheNet:Announce("【 "..GetInstName(picker).." 】从".."〖 "..GetInstName(inst).." 〗上采摘了".."【 "..GetInstName(glommer).." 】的花")
        inst:DoTaskInTime(2,function() if math.random()<.33 then TheNet:Announce("【 "..GetInstName(glommer).." 】貌似蛮喜欢".."【 "..GetInstName(picker).." 】呢！") end end)
    end

    inst.components.pickable.onpickedfn = OnPicked
end)
--赃物袋提示
AddPrefabPostInit("klaus_sack", function (inst)
    local function shanchu(inst)
        local x,y,z=inst.Transform:GetWorldPosition()
        local XianZuoBiao = ("，坐标："..(math.floor(x))..", "..(math.floor(z))) or ""
        TheNet:Announce(world.."〖 "..GetInstName(inst).." 〗被打开"..XianZuoBiao)
    end

    inst:DoTaskInTime(.5, function(inst)
        local x,y,z=inst.Transform:GetWorldPosition()
        local XianZuoBiao = ("，坐标："..(math.floor(x))..", "..(math.floor(z))) or ""
        TheNet:Announce(world.."克劳斯的【 "..GetInstName(inst).." 】出现了！"..XianZuoBiao)
    end)
    inst:ListenForEvent("onremove", shanchu)
end)
























